package org.apache.struts.ehr.medical_records.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis2.AxisFault;
import org.apache.struts.ehr.medical_records.model.DischargePatient;
import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.Query;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.medical_records.model.Ward;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADTA03;
import com.earogya.logic.medical_records.CreateQRYA19;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/***
 * This class is the struts action to discharge a patient. 
 * @author Chamendri
 *
 */
public class DischargePatientAction extends ActionSupport{

	private DischargePatient dischargePatient;
	private HL7Message message;
	private ReplyMessage replyMessage;
	private Ward ward;
	private String occupiedBeds;
	private Boolean isReply=false;
	private Boolean isWardEmpty=false;
	
	public Boolean getIsWardEmpty() {
		return isWardEmpty;
	}

	public Boolean getIsReply() {
		return isReply;
	}


	public String getOccupiedBeds() {
		return occupiedBeds;
	}

	public Ward getWard() {
		return ward;
	}

	public void setWard(Ward ward) {
		this.ward = ward;
	}
	
	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}
	
	public DischargePatient getDischargePatient() {
		return dischargePatient;
	}

	public void setDischargePatient(DischargePatient dischargePatient) {
		this.dischargePatient = dischargePatient;
	}

	public HL7Message getMessage() {
		return message;
	}

	/**
	 * Overrides the execute method is ActionSupport interface.
	 * Creates a ADT A03 message and send to back end.
	 * Parse the reply and display the results.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		replyMessage=new ReplyMessage();
		replyMessage.setMessage(Constants.EMPTY);
		CreateADTA03 createMessage=new CreateADTA03(dischargePatient);
		String msg=createMessage.createMessage(Constants.SENDING_FACILITY);
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			if(!reply.equals(Constants.NO_REPLY)){
				ParseReply parse=new ParseReply();
				replyMessage=(ReplyMessage)parse.parseMessage(reply);
				if(replyMessage.getMessage().equals(Constants.SUCCESS)){
					Query query=new Query();
					query.setPatientId(dischargePatient.getWard());
					query.setSearch_type(Constants.OCCUPIED_BEDS);
					CreateQRYA19 qry=new CreateQRYA19(query);
					String msg1=qry.createMessage(Constants.SENDING_FACILITY);
					if(!msg1.equals(null)){
						Communicator c1=new Communicator();
						try {
							String reply1=c1.sendMessage(msg1);
							if(!reply.equals(Constants.NO_REPLY)){
								ParseReply parse1=new ParseReply();
								occupiedBeds=(String)parse1.parseMessage(reply1);
								if(occupiedBeds!=null){
									isReply=true;
								}else{
									isWardEmpty=true;
									isReply=true;
								}
								ward=new Ward();
								ward.setWardNo(Integer.parseInt(dischargePatient.getWard()));
							}else{
								replyMessage.setMessage(reply1);
							}
						} catch (AxisFault e) {
							replyMessage.setMessage(e.getReason());
							return ERROR;
						} catch (Exception e) {
							replyMessage.setMessage(e.getMessage());
							return ERROR;
						}
					}else{
						replyMessage.setMessage("Failed to create request message");
					}
				}else{
					return ERROR;
				}
			}
			else{
				replyMessage.setMessage(reply);
				return ERROR;
			}
		}
		else{
			replyMessage.setMessage("Failed to create request message");
		}
		replyMessage.setMessage("Patient id "+dischargePatient.getPatientId()+" is successfully discharged!");
		return SUCCESS;
	}
	
	/**
	 * Overrides the validate method is ActionSupport interface.
	 * Validates the input fields in patient discharge form.
	 */
	public void validate(){
		if(dischargePatient.getPatientId()==0){
			addFieldError("dischargePatient.patientId", "Patient ID required");
		}
		if(dischargePatient.getDischargeConsultantId().length()==0){
			addFieldError("dischargePatient.dischargeConsultantId", "Consultant ID required");
		}
		if(dischargePatient.getDischargeDate()==null){
			addFieldError("dischargePatient.dischargeDate", "Date required");
		}
	}
	
}
